#pragma once

#ifndef _MESSAGE_HELPER_H_
#define _MESSAGE_HELPER_H_
serialization& endm( serialization& buf );

serialization& operator << ( serialization& buf, serialization& end( serialization& ) );

template< class message_type >
xgc_void Send( net::network_t h, const message_type &msg, xgc_size alloca_size = MAX_PACKET_SIZE )
{
	xgc_byte *buffer = (xgc_byte*)net::AllocPacket( alloca_size );
	serialization packet( buffer, alloca_size );

	packet << xgc_uint16(0) << msg << endm;
	net::SendPacket( h, packet.base(), packet.wd() );
}

template< class message_type >
xgc_void Broadcast( net::group_t g, const message_type &msg, xgc_size alloca_size )
{
	xgc_byte *buffer = (xgc_byte*)net::AllocPacket( alloca_size );
	serialization packet( buffer, alloca_size );

	packet << xgc_uint16(0) << msg << endm;
	net::SendToGroup( g, packet.base(), packet.wd() );
}

template< class message_type >
xgc_void Read( message_type &msg, const xgc_byte* base, xgc_size size )
{
	serialization packet( base + sizeof(net::MessageHeader), size - sizeof(net::MessageHeader) );
	packet >> msg;
};

#endif // _MESSAGE_HELPER_H_